﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ArbolandiaBL
{
    internal class CategoryProvider
    {
        private ArbolandiaDBDataContext _db;

        public CategoryProvider()
        {
            _db = new ArbolandiaDBDataContext();

        }
        internal void InsertUpdateCategory(Product_Category cat)
        {
            var query = _db.Product_Category.Where(w => w.Id == cat.Id);
            if (query.Count() > 0)
            {
                query.First().FullDescription = cat.FullDescription;
                query.First().MetaKeywords = cat.MetaKeywords;
                _db.SubmitChanges();
            }
            //else
            //{
            //    _db.Product_Category.InsertOnSubmit(cat);
            //}

        }
        internal void DeleteCategory(int id)
        {
            var query = _db.Product_Category.Where(w => w.Id == id);
            if (query.Count() > 0) { _db.Product_Category.DeleteOnSubmit(query.First()); _db.SubmitChanges(); }
        }
        internal List<Product_Category> GetCategorys()
        {
            var query = _db.Product_Category.Select(w => w);
            return query.Count() > 0 ? query.ToList() : new List<Product_Category>();
        }
        internal List<object> GetPictureCategory(Int32 categoryid)
        {
            var res = new List<object>();

            var query = from s in _db.spGetPictureCategory(categoryid)
                        select new
                        {
                            GuidId = s.GuidId,
                            Name = s.Name,
                            url = s.url
                        };
            foreach (var item in query)
            {
                res.Add(new
                {
                    GuidId = item.GuidId,
                    Name = item.Name,
                    url = item.url
                });
            }
            return res;
        }
        internal void InsertPictureCategory(Int32 categoryid, Guid guid, Picture picture)
        {
            //Guardamos la imagen y la damos de alta en arb_picture
            picture.GuidId = guid;
            new ImageProvider().InsertUpdateImage(picture);

            //actualizamos el guid en la tabla de subcategorias
            UpdatePictureGuidCategory(categoryid, guid);
        }
        internal void UpdatePictureGuidCategory(Int32 categoryid, Guid guid)
        {

            var query = _db.Product_Category.Where(w => w.Id == categoryid);
            if (query.Count() > 0)
            {
                query.First().PictureGuidId = guid;
                _db.SubmitChanges();
            }
        }
        internal void DeletePictureCategory(Int32 categoryid)
        {
            var query = from s in _db.Product_Category
                        join p in _db.Picture on s.PictureGuidId equals p.GuidId
                        where s.Id == categoryid
                        select p;
            if (query.Count() > 0)
            {
                _db.Picture.DeleteOnSubmit(query.First());
                _db.SubmitChanges();
            }

        }
        internal Product_Category GetCategoryId(Int32 categoryid)
        {
            var query = _db.Product_Category.Where(w => w.Id == categoryid);
            return query.Count() > 0 ? query.FirstOrDefault<Product_Category>() : new Product_Category();

        }
    }
}
